﻿
#include "man-trigger-sample-v1.h"

namespace man_trigger_sample_v1
{
	class ManTriggerSampleVars : public SessionSampleVars
	{
	public:
		SPADAS_VARS(ManTriggerSample, SessionSampleVars)
		ManTriggerSampleData data;

		Bool supportInterpolation() override
		{
			return FALSE;
		}

		SessionGeneralSample toGeneralSample()
		{
			SessionGeneralSample sample;
			sample.timestamp() = timestamp;
			sample.values() = Array<GeneralElement>(16);

			for (UInt i = 0; i < 16; i++)
			{
				sample.values()[i] = data.triggers[i] ? 1 : 0;
			}

			sample.significantCount() = 16;
			return sample;
		}

		Bool fromSample(String protocol, SessionSample sample) override
		{
			if (!sample.is<SessionGeneralSample>()) return FALSE;

			auto generalSample = sample.as<SessionGeneralSample>();
			timestamp = generalSample.timestamp();
			auto values = generalSample.values();

			if (protocol == "man-trigger-sample-v1")
			{
				if (values.size() != 16) return FALSE;

				for (UInt i = 0; i < 16; i++)
				{
					data.triggers[i] = values[i].value == 1;
				}

				return TRUE;
			}

			return FALSE;
		}
	};
}

using namespace man_trigger_sample_v1;

ManTriggerSample::ManTriggerSample() : Object<ManTriggerSampleVars>(new ManTriggerSampleVars, TRUE)
{}

FullTimestamp& ManTriggerSample::timestamp()
{
	return vars->timestamp;
}

ManTriggerSampleData *ManTriggerSample::operator ->()
{
    return &vars->data;
}

String ManTriggerSample::getProtocolName()
{
    return "man-trigger-sample-v1";
}

SessionGeneralSample ManTriggerSample::toGeneralSample()
{
    return vars->toGeneralSample();
}

Array<String> ManTriggerSample::getProtocolNames()
{
	return Array<String>::create(1, "man-trigger-sample-v1");
}

Bool ManTriggerSample::fromGeneralSample(String protocol, SessionGeneralSample generalSample)
{
    return vars->fromSample(protocol, generalSample.as<SessionSample>());
}